/*实现级联勾选开始*/
var nodeCheckedSilent = false;
function nodeChecked (event, node){
    if(nodeCheckedSilent){
        return;
    }
    nodeCheckedSilent = true;
    checkAllParent(node);
    checkAllSon(node);
    nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked  (event, node){
    if(nodeUncheckedSilent)
        return;
    nodeUncheckedSilent = true;
    uncheckAllParent(node);
    uncheckAllSon(node);
    nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)){
        return;
    }else{
        checkAllParent(parentNode);
    }
}
//取消全部父节点
function uncheckAllParent(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    var siblings = $('#tree').treeview('getSiblings', node.nodeId);
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)) {
        return;
    }
    var isAllUnchecked = true;  //是否全部没选中
    for(var i in siblings){
        if(siblings[i].state.checked){
            isAllUnchecked=false;
            break;
        }
    }
    if(isAllUnchecked){
        uncheckAllParent(parentNode);
    }

}

//级联选中所有子节点
function checkAllSon(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            checkAllSon(node.nodes[i]);
        }
    }
}
//级联取消所有子节点
function uncheckAllSon(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            uncheckAllSon(node.nodes[i]);
        }
    }
}
/*实现级联勾选结束*/

/*----------------------------------*/

/*$(function(){
    initGrant();
});

function initGrant(){
    $("#grant").dialog({
        title:'角色授权',
        btnValue:"授权",
        btnBind:function(){
            var rows=$("#roleTable").bootstrapTable("getSelections");
            var param="roleId="+rows[0].id;
            var arr=$("#tree").treeview("getChecked");//?
            $(arr).each(function(){
                param=param+"&ids="+this.id;
            });
            $.post(ctx+"/sys/permission/grant",param,function(msg){
                toastr.warning("信息提示",msg);
                $("#grant").dialog("close");
                $("#roleTable").bootstrapTable("uncheckAll");
            },'text');

        }
    });
}*/
$(function () {
    $("#grant").dialog({
        title: '角色授权',
        btnValue: "授权",
        btnBind: function () {
            alert("fd");
        }

}) });
function grant(){
    var rows=$("#roleTable").bootstrapTable("getSelections");
    if(rows.length!=1){
        swal("请选择一个角色待授权！");
        return;
    }
    $.getJSON(ctx+"/sys/permission/getTree","roleId="+rows[0].id,function(result){
        $("#tree").treeview({
            data:result,
            showCheckbox:true,
            multiSelect:true,
            onNodeChecked:nodeChecked ,
            onNodeUnchecked:nodeUnchecked
        });
        $("#grant").dialog("open");
    });
}

function getDetail(index,row) {
    var id=row.id;
    var html="";
    $.ajax({
        url:ctx+"/sys/role/searchByRole",
        data:"id="+id,
        dataType:"json",
        async:false,  //false代表同步
        success:function(arr){
            $(arr).each(function(){
                var str="<p>"+this.username+":"+this.note+"</p>"
                html+=str;
            });
        }
    });
    return html;

}